package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.content.mapper.TeachplanMapper;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.model.po.Teachplan;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TeachplanServiceImpl implements TeachplanService {
    @Autowired
    private TeachplanMapper teachplanMapper;
    @Override
    public List<TeachplanDto> findTeachplanTree(Long courseId) {
        return teachplanMapper.selectTreeNodes(courseId);
    }

    @Override
    public void saveTeachplan(SaveTeachplanDto teachplanDto) {
        Long id = teachplanDto.getId();
        if(id==null){
            Teachplan teachplan = new Teachplan();
            BeanUtils.copyProperties(teachplanDto,teachplan);
            Long parentid = teachplanDto.getParentid();
            Long courseId = teachplanDto.getCourseId();
            LambdaQueryWrapper<Teachplan> Wrapper = new LambdaQueryWrapper();
            Wrapper.eq(Teachplan::getParentid,parentid).eq(Teachplan::getCourseId,courseId);
            Integer res = teachplanMapper.selectCount(Wrapper);
            teachplan.setOrderby(res+1);

            teachplanMapper.insert(teachplan);
        }else {
            Teachplan teachplan = teachplanMapper.selectById(teachplanDto.getId());
            BeanUtils.copyProperties(teachplanDto,teachplan);
            teachplanMapper.updateById(teachplan);
        }
    }

    @Override
    public void deleteTeachplan(Long courseId) {
//        List<TeachplanDto> list=teachplanMapper.selectTreeNodes(courseId);
//        for(TeachplanDto item:list){
//            if(item.getParentid()==0){
//
//            }
//        }
    }
}
